//每页多少个物品
const ITEM_PAGE_MAX: number = 49;
//每行多少个格子
const MAX_COLUM: number = 7;
//每页多少行
const MAX_LINE: number = 7;
//格子大小
const BOX_SIZE_X: number = 38;
const BOX_SIZE_Y: number = 37;
//起始坐标
const STAR_X: number = 45;
const STAR_Y: number = 75;

//仓库总格子数  470
const TotalSize: number = 490;

//存物品
export function StoreItems(Player: TPlayObject, Item: TUserItem) {
    if (Item) {
        if (Player.BigStorageItemsCount < TotalSize) {
            Player.AddItemToBigStorage(Item)
        } else {
            Player.MessageBox("你仓库满了！");
        }

        let Args = new TArgs()
        ViewItems(GameLib.QFunctionNpc, Player, Args)
    }

}

export function ViewItems(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    //显示物品的字符串
    let ItemsString = '';
    //当前页显示的物品个数
    let tempStar: number = 0;
    let tempMax: number = 0;
    //当前显示物品的坐标
    let trueX: number = 0;
    let trueY: number = 0;
    //当前显示总数,第几个物品
    let thisPageIndex: number = 0;
    let trueIndex: number = 0;
    //多少行余一个
    let yushu: number = 0;
    let zhengshu: number = 0;
    //当前页
    let tempP: number = Player.GetPVar(297);

    Player.SetPVar(298, 1);//打开仓库

    //计算物品总页数
    let maxPage = Math.ceil(Player.GetBigStorageItemsCount() / ITEM_PAGE_MAX);
    maxPage = Math.max(maxPage, 1);
    // console.log('maxPage= '+maxPage+" ,Player.GetBigStorageItemsCount() = "+Player.GetBigStorageItemsCount());
    //计算当前页开始位置   无限仓库 从 0 开始
    tempStar = ITEM_PAGE_MAX * (tempP - 1);

    //计算当前页最后一个物品位置
    //  49 X (1-1) = 0 第一页 -> 48
    //  49 X (2-1) = 49 第二页 -> 97
    //  49 X (3-1) = 98 第三页 -> 146   .....
    //    100 - 0 = 100   100-97 = 3
    if (Player.GetBigStorageItemsCount() - tempStar >= ITEM_PAGE_MAX) {
        // tempMax = tempStar + ITEM_PAGE_MAX;
        tempMax = ITEM_PAGE_MAX;
    } else {
        //  47 X (1-1) = 0 第一页
        //  tempMax = 31 -0 
        // tempMax = Player.GetBigStorageItemsCount();
        tempMax = Player.GetBigStorageItemsCount() - tempStar;
    }
    // console.log("tempStar:"+tempStar+", tempMax = "+tempMax);

    for (thisPageIndex = 0; thisPageIndex < tempMax; thisPageIndex++) {

        trueIndex = tempStar + thisPageIndex

        let item = Player.GetBigStorageItem(trueIndex)

        yushu = thisPageIndex % MAX_COLUM
        zhengshu = Math.floor(thisPageIndex / MAX_COLUM)

        trueX = STAR_X + yushu * BOX_SIZE_X
        trueY = STAR_Y + zhengshu * BOX_SIZE_Y
        // console.log("trueIndex="+trueIndex+",yushu="+yushu+",zhengshu="+zhengshu+",trueX="+trueX+",trueY="+trueY);

        ItemsString += `\\<{u=${item.MakeString()};x=${trueX};y=${trueY}}/@可视仓库.TakebacItem(${trueIndex})>`

    }
    // console.log(ItemsString);  

    ItemsString += `<{S=${Player.GetPVar(297)}` + ` / ` + `${maxPage}` + ` 页` + `;C=251;X=155;Y=355}>`;
    ItemsString += `<{S=上一页;C=251;X=105;Y=355}/@可视仓库.BeforePage()>`;
    ItemsString += `<{S=下一页;C=251;X=225;Y=355}/@可视仓库.NextPage()>`;
    ItemsString += `<{I=529;F=Prguse.data;X=295;Y=355}/@可视仓库.CloseViewItems()>`;
    //  console.log(ItemsString)
    Npc.SayEx(Player, "分页仓库", ItemsString);
    Player.OpenBag();

}

//取出
export function TakebacItem(Npc: TNormNpc, Player: TPlayObject, Args: TArgs): void {
    let ID = Args.Int[0]
    // console.log("Player.MaxBagSize=" + Player.MaxBagSize + ", Player.ItemSize=" + Player.ItemSize)
    if (Player.GetMaxBagSize() < Player.GetItemSize()) {
        Player.SendMessage('物品取出失败，背包满了', 1)
    } else if (Player.TakebackBigStorageItem(ID)) {
        ViewItems(Npc, Player, Args)
    } else {
        Player.SendMessage('物品取出失败，看看你背包、负重', 1)
    }

}


//上一页
export function BeforePage(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    if (Player.GetPVar(297) <= 1) {
        Player.SendMessage('当前已经是第一页了', 1)
    } else {
        Player.SetPVar(297, Player.GetPVar(297) - 1)
        ViewItems(Npc, Player, Args)
    }
    //  console.log("BeforePage- 298 = "+Player.GetPVar(298)+", 297 = "+Player.GetPVar(297));
}

//下一页
export function NextPage(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    //计算物品总页数
    let maxPage = Math.ceil(Player.GetBigStorageItemsCount() / ITEM_PAGE_MAX);
    maxPage = Math.max(maxPage, 1);

    if (Player.GetPVar(297) >= maxPage) {
        Player.SendMessage('当前已经是最后一页了', 1)
    } else {

        Player.SetPVar(297, Player.GetPVar(297) + 1)
        ViewItems(Npc, Player, Args)
    }
    // console.log("NextPage- 298 = "+Player.GetPVar(298)+", 297 = "+Player.GetPVar(297));
}

//关闭窗口
export function CloseViewItems(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.CloseWindow("分页仓库")
    Player.SetPVar(298, 0);//关闭仓库
    Player.SetPVar(297, 1);//仓库第一页
    // console.log("关闭分页仓库- 298 = " + Player.GetPVar(298) + ", 297 = " + Player.GetPVar(297));
}


export function Main(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.SetPVar(298, 1);//打开仓库
    Player.SetPVar(297, 1);//仓库第一页
    ViewItems(Npc, Player, Args)
    // console.log("打开分页仓库- 298 = " + Player.GetPVar(298) + ", 297 = " + Player.GetPVar(297));
}



